<!DOCTYPE HTML>
<html lang="cn" class="light sidebar-visible" dir="ltr">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>Yarn安装部署 - easy-to-code</title>


        <!-- Custom HTML head -->

        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff">

        <link rel="icon" href="favicon.svg">
        <link rel="shortcut icon" href="favicon.png">
        <link rel="stylesheet" href="css/variables.css">
        <link rel="stylesheet" href="css/general.css">
        <link rel="stylesheet" href="css/chrome.css">
        <link rel="stylesheet" href="css/print.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="fonts/fonts.css">

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" id="highlight-css" href="highlight.css">
        <link rel="stylesheet" id="tomorrow-night-css" href="tomorrow-night.css">
        <link rel="stylesheet" id="ayu-highlight-css" href="ayu-highlight.css">

        <!-- Custom theme stylesheets -->


        <!-- Provide site root and default themes to javascript -->
        <script>
            const path_to_root = "";
            const default_light_theme = "light";
            const default_dark_theme = "navy";
        </script>
        <!-- Start loading toc.js asap -->
        <script src="toc.js"></script>
    </head>
    <body>
    <div id="mdbook-help-container">
        <div id="mdbook-help-popup">
            <h2 class="mdbook-help-title">Keyboard shortcuts</h2>
            <div>
                <p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
                <p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
                <p>Press <kbd>?</kbd> to show this help</p>
                <p>Press <kbd>Esc</kbd> to hide this help</p>
            </div>
        </div>
    </div>
    <div id="body-container">
        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script>
            try {
                let theme = localStorage.getItem('mdbook-theme');
                let sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script>
            const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
            let theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            const html = document.documentElement;
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add("js");
        </script>

        <input type="checkbox" id="sidebar-toggle-anchor" class="hidden">

        <!-- Hide / unhide sidebar before it is displayed -->
        <script>
            let sidebar = null;
            const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            } else {
                sidebar = 'hidden';
            }
            sidebar_toggle.checked = sidebar === 'visible';
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <!-- populated by js -->
            <mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
            <noscript>
                <iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
            </noscript>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle">
                <div class="sidebar-resize-indicator"></div>
            </div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky">
                    <div class="left-buttons">
                        <label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </label>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">easy-to-code</h1>

                    <div class="right-buttons">
                        <a href="print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>

                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script>
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <h1 id="yarn安装部署"><a class="header" href="#yarn安装部署">Yarn安装部署</a></h1>
<h2 id="yarn的简介"><a class="header" href="#yarn的简介">yarn的简介</a></h2>
<p>Yarn是facebook发布的一款取代npm的包管理工具。</p>
<h2 id="安装"><a class="header" href="#安装">安装</a></h2>
<h3 id="下载nodejs使用npm安装"><a class="header" href="#下载nodejs使用npm安装">下载node.js，使用npm安装</a></h3>
<pre><code>npm install -g yarn 
</code></pre>
<h3 id="查看版本"><a class="header" href="#查看版本">查看版本</a></h3>
<pre><code>yarn --version
</code></pre>
<h2 id="一顿操作"><a class="header" href="#一顿操作">一顿操作</a></h2>
<pre><code>//Yarn淘宝源安装
yarn config set registry https://registry.npm.taobao.org -g 
yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass -g

//魔法代理，你懂的
yarn config set https-proxy http://127.0.0.1:7890
yarn config set proxy http://127.0.0.1:7890

//设置缓存目录
yarn config set cache-folder "F:/ProgramFiles/NODE_REPO/yarn_cache"

//设置本地依赖目录
yarn config set yarn-offline-mirror "F:/ProgramFiles/NODE_REPO/yarn_offline_mirror"

//使缓存文件夹保持最新
yarn config set yarn-offline-mirror-pruning true

//重新设置https-proxy、proxy
yarn config delete https-proxy
yarn config delete proxy
yarn config get https-proxy
yarn config get proxy
</code></pre>
<h3 id="查看配置"><a class="header" href="#查看配置">查看配置</a></h3>
<pre><code>// 显示所有配置项
yarn config list

//查看代理情况
yarn config get https-proxy 
yarn config get proxy 
</code></pre>
<h3 id="证书过期怎么办"><a class="header" href="#证书过期怎么办">证书过期怎么办</a></h3>
<p>运行yarn install 报错提示：</p>
<p>yarn install  Error: certificate has expired</p>
<pre><code>yarn config set strict-ssl false
</code></pre>
<h2 id="yarn的特点"><a class="header" href="#yarn的特点">yarn的特点</a></h2>
<p>速度超快</p>
<p>Yarn 缓存了每个下载过的包，所以再次使用时无需重复下载。 同时利用并行下载以最大化资源利用率，因此安装速度更快</p>
<p>超级安全</p>
<p>在执行代码之前，Yarn 会通过算法校验每个安装包的完整性</p>
<p>超级可靠</p>
<p>使用详细、简洁的锁文件格式和明确的安装算法，Yarn 能够保证在不同系统上无差异的工作。</p>
<h2 id="yarn的常用命令"><a class="header" href="#yarn的常用命令">yarn的常用命令</a></h2>
<p>安装yarn</p>
<p>npm install -g yarn</p>
<p>安装成功后，查看版本号</p>
<p>yarn --version</p>
<p>创建文件夹 yarn</p>
<p>md yarn</p>
<p>进入yarn文件夹</p>
<p>cd yarn</p>
<p>初始化项目</p>
<p>yarn init // 同npm init，执行输入信息后，会生成package.json文件</p>
<p>yarn的配置项</p>
<p>yarn config list // 显示所有配置项</p>
<p>yarn config get <key> //显示某配置项</p>
<p>yarn config delete <key> //删除某配置项</p>
<p>yarn config set <key> <value> [-g|--global] //设置配置项</p>
<p>安装包</p>
<p>yarn install //安装package.json里所有包，并将包及它的所有依赖项保存进yarn.lock
yarn install --flat //安装一个包的单一版本
yarn install --force //强制重新下载所有包
yarn install --production //只安装dependencies里的包
yarn install --no-lockfile //不读取或生成yarn.lock
yarn install --pure-lockfile //不生成yarn.lock
yarn install --offline  //从缓存离线安装依赖</p>
<p>添加包（会更新package.json和yarn.lock）</p>
<p>yarn add [package] // 在当前的项目中添加一个依赖包，会自动更新到package.json和yarn.lock文件中</p>
<p>yarn add [package]@[version] // 安装指定版本，这里指的是主要版本，如果需要精确到小版本，使用-E参数</p>
<p>yarn add [package]@[tag] // 安装某个tag（比如beta,next或者latest）</p>
<p>//不指定依赖类型默认安装到dependencies里，你也可以指定依赖类型</p>
<p>yarn add --dev/-D // 加到 devDependencies</p>
<p>yarn add --peer/-P // 加到 peerDependencies</p>
<p>yarn add --optional/-O // 加到 optionalDependencies</p>
<p>//默认安装包的主要版本里的最新版本，下面两个命令可以指定版本</p>
<p>yarn add --exact/-E // 安装包的精确版本。例如yarn add foo@1.2.3会接受1.9.1版，但是yarn add foo@1.2.3 --exact只会接受1.2.3版</p>
<p>yarn add --tilde/-T // 安装包的次要版本里的最新版。例如yarn add foo@1.2.3 --tilde会接受1.2.9，但不接受1.3.0</p>
<p>发布包</p>
<p>yarn publish</p>
<p>移除一个包</p>
<p>yarn remove <packageName>：移除一个包，会自动更新package.json和yarn.lock</p>
<p>更新一个依赖</p>
<p>yarn upgrade 用于更新包到基于规范范围的最新版本</p>
<p>运行脚本</p>
<p>yarn run 用来执行在 package.json 中 scripts 属性下定义的脚本</p>
<p>显示某个包的信息</p>
<p>yarn info <packageName> 可以用来查看某个模块的最新版本信息</p>
<p>缓存</p>
<p>yarn cache</p>
<p>yarn cache list # 列出已缓存的每个包</p>
<p>yarn cache dir # 返回 全局缓存位置</p>
<p>yarn cache clean # 清除缓存</p>
<h2 id="npm-与-yarn命令比较"><a class="header" href="#npm-与-yarn命令比较">npm 与 yarn命令比较</a></h2>
<p>比如说你的项目模块依赖是图中描述的，@1.2.1代表这个模块的版本。在你安装A的时候需要安装依赖C和D，很多依赖不会指定版本号，默认会安装最新的版本，这样就会出现问题：比如今天安装模块的时候C和D是某一个版本，而当以后C、D更新的时候，再次安装模块就会安装C和D的最新版本，如果新的版本无法兼容你的项目，你的程序可能就会出BUG，甚至无法运行。这就是npm的弊端，而yarn为了解决这个问题推出了yarn.lock的机制，这是作者项目中的yarn.lock文件。</p>
<p>注意：这个文件不要手动修改它，当你使用一些操作如yarn add时，yarn会自动更新yarn.lock</p>
<p>参考：https://blog.csdn.net/weixin_40808668/article/details/122606543</p>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                            <a rel="prev" href="Node_dev.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>

                            <a rel="next prefetch" href="Vue_dev.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                    <a rel="prev" href="Node_dev.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>

                    <a rel="next prefetch" href="Vue_dev.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>




        <script>
            window.playground_copyable = true;
        </script>


        <script src="elasticlunr.min.js"></script>
        <script src="mark.min.js"></script>
        <script src="searcher.js"></script>

        <script src="clipboard.min.js"></script>
        <script src="highlight.js"></script>
        <script src="book.js"></script>

        <!-- Custom JS scripts -->


    </div>
    </body>
</html>
